<?php

require_once('conf/DBConnection.php');
require_once('dao/DAO.php');
require_once('search/ImagenesSearchCriteria.php');


/**
 * DAO is actually a J2EE  pattern. 
 * It can easily be implemented in PHP and helps greatly in separating database access from the rest of your code. 
 * The DAOs form a thin layer. The DAO layer can be 'stacked' which helps for instance if you want to add DB 
 * caching later when tuning your application. You should have one DAO class for every VO class. 
 * Naming conventions are a good practice. 
 * 
 * @author dintech
 *
 */

class ImagenesDAO extends DAO {

	function ImagenesDAO() {}

	function get($id) {
		DBConnection::getInstance();
		$query = "SELECT * FROM `imagenes` I where $id = id";
		$result = mysql_query($query);
		$results = $this->getFromResult($result);
		return $results[0];
	}

	function delete($id) {
		DBConnection::getInstance();
		$query = "DELETE from `imagenes` where id = $id";
		$result = mysql_query($query);
		return parent::checkError($result);
	}

	function update($vo) {
		DBConnection::getInstance();
		$query = "UPDATE imagenes SET ".
			" description ='".$vo->getDescription()."'".
			" WHERE id =".$vo->getId();
		//echo $query;
		$result = mysql_query($query);
		return parent::checkError($result);
	}

	function insert(&$vo) {
		DBConnection::getInstance();
		$query = "INSERT INTO `imagenes` (`id`, `filename`, `tablename`, `iditem`, `date`, `order`, `width`, `height`, `size`, `alt`, `description`) values (NULL, '".$vo->getFilename()."', '".$vo->getTable()."', '".$vo->getIditem()."', '".$vo->getDate()."', '".$vo->getOrder()."', '".$vo->getWidth()."', '".$vo->getHeight()."', '".$vo->getSize()."', '".$vo->getAlt()."', '".$vo->getDescription()."' )";
		$result = mysql_query($query);
		return parent::checkError($result);
	}
	
	function find($criteria){
		DBConnection::getInstance();
		$conditions = $this->getConditions($criteria);
		$query = "SELECT * FROM `imagenes` I where 1 $conditions";
		$result = mysql_query($query);	
		return $this->getFromResult($result);
	}
	
	function getLastInsertId() {
		DBConnection::getInstance();
		$query = "SELECT LAST_INSERT_ID() as last_id";
		$result = mysql_query($query);
		$results = $this->getFromResult($result);
		return $results[0][last_id];		
	}
	
	function getConditions($criteria){
		if ($criteria->getIdItem() != null){
			$conditions.= " AND iditem = '".$criteria->getIditem()."'";
		}
		if ($criteria->getFecha() != null){
			$conditions.= " AND fecha = '".$criteria->getFecha()."'";
		}
		if ($criteria->getTable() != null){
			$conditions.= " AND tablename = '".$criteria->getTable()."'";
		}
		return $conditions; 
	}
	
	/**
	 * Actualiza el imagesid FK con el id de la imagen
	 * y el imagecount de una propiedad por id
	 * El valor de $valueIncrDec puede ser +1 o -1, dependiendo
	 * de si se agregue o elimine una imagen
	 * @param ID de la propiedad $id
	 * @param Integer $imagesId (FK)
	 * @param Integer $valueIncrDec
	 */
	function updateImageinfo($table, $id, $imagesId, $valueIncrDec = 1) {
		DBConnection::getInstance();
	
		// si la propiedad no tiene  ($imagecount=0), setear
		// como $imagesid la actual
		if ($imagesId != null	) {
			$query = "UPDATE $table SET "
					." imagesid=".$imagesId
					." WHERE id=".$id;
			echo $query;
			$result = mysql_query($query);
		}
	
		// modifica valor de imagecount
		$query = "UPDATE $table SET "
				." imagecount=imagecount+(".$valueIncrDec.")"
						." WHERE id=".$id;
		//echo "<br/>".$query;exit;
		$result = mysql_query($query);
	
		return parent::checkError($result);
	}
	
	function getEntidad($table, $iditem) {
		DBConnection::getInstance();
		$query = "SELECT * FROM $table where id = $iditem";
		$result = mysql_query($query);
		$results = $this->getFromResult($result);
		return $results[0];
	}
	
}
?>